<!--基础的战绩展示卡片 数据为  一个对局的所有完整数据-->
<script setup lang="js">

import {onBeforeMount, reactive, ref} from "vue";
import {gameModeMap, gameResultWinMap, queueMap} from "@/renderer/share/utils/constants.js";
import moment from "moment";
import {champion_store, map_store, staticAssertsStore} from "@/renderer/main-window/stores/storeAsserts.js";
import {getSingleGameRecordInfo} from "@/renderer/share/utils/rank.js";

const {summonerInfo, singleRecord} = defineProps({
  //当前登录人的信息
  "summonerInfo": {},
  //单条对局记录粗略数据
  "singleRecord": {}
})

const gameRecordDetail = ref({});
const totalTeamInfos = ref([])
const participantsummonerInfoMap = new Map();
const participantGameInfoMap = new Map();
const puuIdParticipantMap = new Map();
const teamMap = new Map();
const teamMapSortMap = new Map();


//查询单个对局的战绩
const setGameDetailFunction = () => {
  const {gameId} = singleRecord || {};
  if (gameId === undefined) {
    return
  }
  window.api.gamereocrd.searchGameDetailByGameId({
    "gameId": gameId,
  }).then(res => {
    gameRecordDetail.value = res.data;

    //计算map
    gameRecordDetail.value?.participantIdentities.forEach(item => {
      const {participantId, player} = item;
      const {puuid} = player;
      participantsummonerInfoMap.set(participantId, player);
      puuIdParticipantMap.set(puuid, participantId);
    })

    //游戏信息map
    gameRecordDetail.value?.participants.forEach(item => {
      participantGameInfoMap.set(item.participantId, item);
    })

    gameRecordDetail.value?.teams?.forEach((team, idx) => {
      teamMap.set(team.teamId, team);
      teamMapSortMap.set(team.teamId, idx);
    })


    currentParticipantInfo.value = {...buildCurrentParticipantInfoFunc()}
    participantInfos.dataList = [...buildTotalParticipantInfoFunc()]
    totalTeamInfos.value = [...gameRecordDetail.value?.teams]
  })


}


//能力
const buildSingleParticipantInfoFunc = (puuid) => {
  const participantId = puuIdParticipantMap.get(puuid);
  const player = participantsummonerInfoMap.get(participantId);
  const {
    championId,
    spell1Id,
    spell2Id,
    teamId,
    stats
  } = (participantGameInfoMap.get(participantId)) ?? {};

  const {
    champLevel, //英雄等级
    goldEarned, //获取金币
    wardsKilled,//排眼
    visionScore,//视野得分
    visionWardsBoughtInGame,//真眼数量
    perkPrimaryStyle,//主天赋id
    perkSubStyle,//副天赋ID
    perk0,//主天赋 第一行
    perk0Var1,//主天赋 第1行
    perk0Var2,//主天赋 第2行
    perk0Var3,//主天赋 第3行,
    perk1,
    perk1Var1,
    perk1Var2,
    perk1Var3,
    perk2,
    perk2Var1,
    perk2Var2,
    perk2Var3,
    perk3,
    perk3Var1,
    perk3Var2,
    perk3Var3,
    perk4,
    perk4Var1,
    perk4Var2,
    perk4Var3,
    perk5,
    perk5Var1,
    perk5Var2,
    perk5Var3,
    subteamPlacement,//斗魂竞技场 队伍排名


  } = stats;
  const minute = (singleRecord?.gameDuration / 60).toFixed(0)
  const second = singleRecord?.gameDuration % 60;
  const teamInfo = teamMap.get(teamId);
  const championName = staticAssertsStore().getAssertSync({key: champion_store, id: championId})?.name

  const mapName= staticAssertsStore().getAssertSync({key: map_store, id: singleRecord?.mapId})?.name

  return {
    "player": player,
    "champion": {
      "championId": championId,
      "championName": championName,
      "spell": {
        d: spell1Id,
        f: spell2Id
      },
      //天赋
      "perkStyle": {
        perkPrimaryStyle:perkPrimaryStyle,
        perkSubStyle:perkSubStyle,
        perk0:perk0,
        perk0Var1:perk0Var1,
        perk0Var2:perk0Var2,
        perk0Var3:perk0Var3,
        perk1:perk1,
        perk1Var1:perk1Var1,
        perk1Var2:perk1Var2,
        perk1Var3:perk1Var3,
        perk2:perk2,
        perk2Var1:perk2Var1,
        perk2Var2:perk2Var2,
        perk2Var3:perk2Var3,

        perk3:perk3,
        perk3Var1:perk3Var1,
        perk3Var2:perk3Var2,
        perk3Var3:perk3Var3,

        perk4:perk4,
        perk4Var1:perk4Var1,
        perk4Var2:perk4Var2,
        perk4Var3:perk4Var3,

        perk5:perk5,
        perk5Var1:perk5Var1,
        perk5Var2:perk5Var2,
        perk5Var3:perk5Var3
      },
      "items": {
        item0: stats?.item0,
        item1: stats?.item1,
        item2: stats?.item2,
        item3: stats?.item3,
        item4: stats?.item4,
        item5: stats?.item5,
        item6: stats?.item6

      }
    },
    "data": {
      "kda": {
        k: stats?.kills,
        d: stats?.deaths,
        a: stats?.assists
      },
      //总输出伤害
      "totalDamageDealtToChampions": stats?.totalDamageDealtToChampions,
      //总承受伤害
      "totalDamageTaken": stats?.totalDamageTaken,
      "champLevel": champLevel,
      "goldEarned": goldEarned,
      "wardsKilled": wardsKilled,//摧毁守卫
      "visionScore": visionScore,//视野得分
      "visionWardsBoughtInGame": visionWardsBoughtInGame,//购买真眼
    },
    "tags": [],
    "baseInfo": {
      "queue": {
        "queueId": singleRecord?.queueId,
        "queueName":queueMap[singleRecord?.queueId],
        "gameMode":singleRecord.gameMode,
      },
      mapInfo:{
        mapId:singleRecord?.mapId,
        mapName:mapName,
      },
      "useTimeText": `${minute}分${second}秒`,
      "isWin": gameResultWinMap[teamInfo?.win],
      "win": teamInfo?.win,
      "gameStartTime": moment.unix(gameRecordDetail.value.gameCreation / 1000).format("MM-DD HH:mm"),
    },
    "teamInfo": teamInfo,
    subteamPlacement:subteamPlacement,
  }
}

//各种透给子模块的数据
//当前用户的数据
const currentParticipantInfo = ref({});
const buildCurrentParticipantInfoFunc = () => {
  const participantId = puuIdParticipantMap.get(summonerInfo.puuid);
  const player = participantsummonerInfoMap.get(participantId);
  return {
    "summonerInfo": player,
    "participantId": participantId,
    "info": buildSingleParticipantInfoFunc(summonerInfo.puuid)
  }
}

//用户的全部信息
const participantInfos = reactive({"dataList": []});
const buildTotalParticipantInfoFunc = () => {
  let arr = (gameRecordDetail.value?.participantIdentities);
  if (arr === undefined) {
    return [];
  }


  const result = [];

  arr.map(item => {
    const puuid = participantsummonerInfoMap.get(item.participantId)?.puuid
    return buildSingleParticipantInfoFunc(puuid);
  }).forEach(item => {
    if(gameRecordDetail?.value?.gameMode==='CHERRY'){
      const idx = item?.subteamPlacement-1;
      if(!result[idx]){
        result[idx] = [];
      }
      result[idx].push(item);
    }else{
      const idx = teamMapSortMap.get(item?.teamInfo?.teamId);
      if (!result[idx]) {
        result[idx] = [];
      }
      result[idx].push(item);
    }


  })

  return result;
}


//组件初始化
onBeforeMount(() => {
  setGameDetailFunction();
 // getSingleGameRecordInfo({
 //   'summonerInfo':summonerInfo,
 //   'singleRecord':singleRecord}).then((res)=>{
 //   console.log(res)
 //     }
 //  )

})

</script>

<template>
  <slot
      :currentParticipantInfo="currentParticipantInfo"
      :participantInfos="participantInfos.dataList"
      :totalTeamInfos="totalTeamInfos"
  ></slot>
</template>

<style scoped>

</style>